package org.come.baitan;

import com.tool.Stall.Commodity;
import com.tool.tcpimg.UIUtils;
import org.come.Frame.ZhuFrame;
import org.come.until.CutButtonImage;
import org.come.until.FormsManagement;
import org.come.until.GoodsListFromServerUntil;
import org.come.until.Util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.math.BigDecimal;

/**
 * @author 式
 * @version 1.0
 * @project test1
 * @description 道具面板
 * @date 2024/1/31 00:18:00
 */
public class MaiGoodsJpanel extends JLabel {
	Commodity goodstable;
	ImageIcon imageIcon;
private boolean isSelected = false;
int cao=-1;
 ImageIcon img;
ImageIcon path1;
ImageIcon	good;
ShouGou shouGou;
public MaiGoodsJpanel(Commodity commodity, ImageIcon imageIcon,int cao,int x,int y){
		this.setOpaque(false);
		this.cao=cao;
		this.imageIcon=imageIcon;
		this.goodstable=commodity;
		if (cao==2){
			img= new ImageIcon("img/newHead/p" + goodstable.getPet().getSummoningskin() + ".png");
			path1 = CutButtonImage.getImage("inkimg/danxin/ss291.png", 45, 45);
			img.setImage(img.getImage().getScaledInstance(35, 35, 10));
			path1.setImage(path1.getImage().getScaledInstance(45, 45, 10));
			
		}else if (cao==1){
			if (goodstable.getGood()!=null)
			good= GoodsListFromServerUntil.imgpath(goodstable.getGood().getSkin());
		}

	/* // 重新绘制*/
		this.setPreferredSize(new Dimension(x,y));
		this.addMouseListener(new MouseListener() {
			@Override
			public void mouseClicked(MouseEvent e) {
				updateSelectedState();
				isSelected = !isSelected; // 切换选中状态
				repaint();
				GouMaiJpanel.good=goodstable;
				if (cao==2){
					ImageIcon img= new ImageIcon("img/newHead/p" + goodstable.getPet().getSummoningskin() + ".png");
					ImageIcon	path1 = CutButtonImage.getImage("inkimg/danxin/ss291.png", 45, 45);
					img.setImage(img.getImage().getScaledInstance(35, 35, 10));
					path1.setImage(path1.getImage().getScaledInstance(45, 45, 10));
					GouMaiJpanel.img=img;
					GouMaiJpanel.path1=path1;
				}
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
			
			}
			
			@Override
			public void mouseReleased(MouseEvent e) {
				FormsManagement.HideForm(46);
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				if (cao==1){
					if (goodstable.getGood()!=null){
						ZhuFrame.getZhuJpanel().creatgoodtext2(goodstable.getGood());
					}
				}else if (cao==2){
				   if (goodstable.getPet()!=null){
				   	ZhuFrame.getZhuJpanel().creatpettext(goodstable.getPet());
				   }
				}
				
			
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				if (cao==1)
				FormsManagement.HideForm(24);
				else
				FormsManagement.HideForm(42);
			}
		});
	}
	public MaiGoodsJpanel(ShouGou shouGou, ImageIcon imageIcon,int cao,int x,int y){
		this.setOpaque(false);
		this.cao=cao;
		this.imageIcon=imageIcon;
		this.shouGou=shouGou;
		if (shouGou.getGoodstable()!=null)
			good= GoodsListFromServerUntil.imgpath(shouGou.getGoodstable().getSkin());
		this.setPreferredSize(new Dimension(x,y));
		this.addMouseListener(new MouseListener() {
			@Override
			public void mouseClicked(MouseEvent e) {
				updateSelectedState();
				isSelected = !isSelected; // 切换选中状态
				repaint();
				GouMaiJpanel.shougou=shouGou;
				
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
			
			}
			
			@Override
			public void mouseReleased(MouseEvent e) {
				FormsManagement.HideForm(46);
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				if (cao==3){
					if (shouGou.getGoodstable()!=null){
						ZhuFrame.getZhuJpanel().creatgoodtext2(shouGou.getGoodstable());
					}
				}
				
				
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
					FormsManagement.HideForm(24);
			}
		});
	}
private void updateSelectedState() {
	// 遍历所有兄弟面板，重置选中状态
	Container parent = getParent();
	if (parent instanceof JPanel) {
		Component[] components = ((JPanel) parent).getComponents();
		for (Component component : components) {
			if (component instanceof MaiGoodsJpanel && component != this) {
				((MaiGoodsJpanel) component).isSelected = false;
				//GouMaiJpanel.good= ((MaiGoodsJpanel) component).goodstable;
			}
		}
	}
}
@Override
protected void paintComponent(Graphics g) {
	super.paintComponent(g);
	if (cao==1){
		g.drawImage(imageIcon.getImage(),0,0,148,53,null);
		
		g.drawImage(good.getImage(), 8, 8, 40, 40, null);
		{
			g.setColor(Color.black);
			g.drawString(goodstable.getGood().getGoodsname(),55,18);
		}
		this.setFont(UIUtils.TEXT_FONT1);
		{
			g.setColor(Color.white);
			g.drawString(goodstable.getNumber()+"",8,16);
		}
		
		
		Util.drawPrice(g, new BigDecimal(goodstable.getMoney()), 55, 40);
		if (isSelected) {
			g.setColor(Color.RED);
			g.drawRect(0, 0, getWidth() - 1, 53);
		}
	}else if (cao==2){
		g.drawImage(imageIcon.getImage(),0,0,300,76,null);
		if (isSelected) {
			g.setColor(Color.RED);
			g.drawRect(0, 0, getWidth() - 1, getHeight()-1);
		}
		g.drawImage(img.getImage(),22,20,35,35,null);
		g.drawImage(path1.getImage(),17,15,45,45,null);
		Util.drawPrice(g, new BigDecimal(goodstable.getMoney()), 110, 57);
		g.setColor(Color.black);
		g.drawString(goodstable.getPet().getSummoningname(),80,30);
		g.setColor(changeGradeC(goodstable.getPet().getGrade()));
		g.drawString(changeGrade(goodstable.getPet().getGrade()),220,30);
	}else if (cao==3){
		g.drawImage(imageIcon.getImage(),0,0,198,54,null);
		if (isSelected) {
			g.setColor(Color.RED);
			g.drawRect(0, 0, getWidth() - 1, getHeight()-1);
		}
		g.drawImage(good.getImage(), 10, 8, 40, 40, null);
		{
			g.setColor(Color.black);
			g.drawString(shouGou.getGoodstable().getGoodsname(),55,18);
		}
		this.setFont(UIUtils.TEXT_FONT1);
		{
			g.setColor(Color.white);
			g.drawString(shouGou.getNum()+"",8,16);
		}
		Util.drawPrice(g, new BigDecimal(shouGou.getPice()), 55, 40);
	}
	
}
public static String changeGrade(final int grade) {
	if (grade <= 100) {
		
		return "0转" + grade + "级";
		
	}
	else {
		if (grade > 100 && grade <= 221) {
			
			return "1转" + (grade - 101) + "级";
			
		}
		else {
			if (grade > 221 && grade <= 362) {
				
				return "2转" + (grade - 222) + "级";
			}
			else {
				if (grade > 362 && grade <= 543) {
					
					return "3转" + (grade - 363) + "级";
				}
				else {
					return "点化" + (grade - 544) + "级";
				}
			}
		}
	}
}
public static Color changeGradeC(final int grade) {
	if (grade <= 100) {
		return new Color(0, 255, 0);
		
	}
	else {
		if (grade > 100 && grade <= 221) {
			return new Color(255, 140, 0);
			
		}
		else {
			if (grade > 221 && grade <= 362) {
				return new Color(0, 245, 255);
			}
			else {
				if (grade > 362 && grade <= 543) {
					return new Color(238, 44, 44);
				}
				else {
					
					return new Color(138, 43, 226);
				}
			}
		}
	}
}
}
